package com.hejing.Collection子接口Set;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * ClassName: DuplicateTest
 * Package: com.hejing.Collection子接口Set
 * Description:
 *
 * @Author: HeJing
 * @Create: 2023-05-18 20:28
 * @Version: 1.0
 */
public class DuplicateTest {
    public static void main(String[] args) {

        ArrayList list = new ArrayList();
        list.add(34);
        list.add(34);
        list.add(23);
        list.add(23);
        list.add(45);
        list.add(45);

        List newList = duplicateList02(list);
        System.out.println(newList);
    }

    /**
     * 集合去重方式1:
     *
     * @param list
     * @return
     */
    public static List duplicateList01(List list) {
        HashSet set = new HashSet();
        for(Object obj: list) {
            set.add(obj);
        }
        ArrayList list01 = new ArrayList();
        for(Object obj: set) {
            list01.add(obj);
        }
        return list01;
    }

    /**
     * 集合元素去重方式2
     *
     * @param list
     * @return
     */
    public static List duplicateList02(List list) {
        HashSet set = new HashSet(list);
        ArrayList list01 = new ArrayList(set);
        return list01;
    }

}























